package com.huluobo.document

import com.huluobo.document.entity.DocEntity
import com.huluobo.lib_base.BaseRepo
import com.huluobo.lib_base.Res
import com.huluobo.lib_http.HttpType
import com.huluobo.lib_http.NetworkFactory
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

/**
 *  Created by LC on 2025/2/6.
 */
class DocRepo @Inject constructor() : BaseRepo() {
    val apiService by lazy { NetworkFactory.factory(HttpType.NONE).create(ApiService::class.java) }

    fun getDoc(): Flow<Res<DocEntity>> {
        return apiService.getDoc()
    }

    fun deleteDoc(id: Int): Flow<Res<String?>> {
        return apiService.deleteDoc(id)
    }

    fun uploadDoc(name: String, path: String): Flow<Res<String>> {
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(path))
        val multipartBody = MultipartBody.Builder().addFormDataPart("file", name, body).build()
        return apiService.uploadDoc(multipartBody.parts)
    }
}